package cn.zhl.leetcode.链表;

/**
 * @program: demos
 * @description: https://leetcode-cn.com/problems/remove-linked-list-elements/
 * @author: 刘振华
 * @create: 2020-04-03 11:26
 **/
public class _203_移除链表元素 {
	//    删除链表中等于给定值 val 的所有节点。
//
//    示例:
//
//    输入: 1->2->6->3->4->5->6, val = 6
//    输出: 1->2->3->4->5
	public static ListNode removeElements(ListNode head, int val) {
		if (head == null) {
			return head;
		}
		ListNode node = head;
		while (node.next != null) {
			if (val == node.next.val) {
				node.next = node.next.next;
			} else {
				node = node.next;
			}
		}
		if (head.val == val) {
			head = head.next;
		}
		return head;
	}

	public static void main(String[] args) {
//        int[] ints = {1,1,1,1,1,1,1};
		int[] ints = {1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5};
		ListNode node = ListNode.head(ints);


		node = removeElements(node, 1);

		while (node != null) {
			System.out.println(node.val);
			node = node.next;
		}
	}
}
